<template>
	<view>
		<view class="search">
			<uni-search-bar class="input" @clear="clear" @input="input" :radius="30" bgColor="#fff" cancelButton="none"
				placeholder="请输入搜索内容"></uni-search-bar>
		</view>

		<!-- 搜索历史记录 -->
		<view class="history" v-if="searchList.length == 0">
			<view class="history-title">
				<view>搜索历史</view>
				<uni-icons type="trash" size="30" @click="clearHistory"></uni-icons>
			</view>
			<view class="history-list">
				<view class="history-list-title" v-for="(item,index) in historyCom" :key="index">
					<view class="history-name" @click="goGoodsList(item)">{{item}}</view>
				</view>
			</view>

		</view>
		<!-- 搜索的商品列表 -->
		<view class="search_list" v-else>
			<view class="search_title" v-for="(item,index) in searchList" :key="index" @click="goGoodsList">
				<view class="search_goods_name">{{item.goods_name}}</view>
				<uni-icons type="right" size="30"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		search
	} from "@/api/search.js"
	export default {
		data() {
			return {
				timer: null,
				keyword: '',
				searchList: [],
				history: []
			}
		},
		onLoad() {
			this.history = JSON.parse(uni.getStorageSync("keyword") || '[]')
		},
		computed: {
			historyCom() {
				// 在history数组外面套一个数组，然后将该数组反转过来
				return [...this.history].reverse()
			}
		},
		methods: {
			input(e) {
				// 设置一个定时器，实现一个函数防抖效果
				clearTimeout(this.timer);
				this.timer = setTimeout(() => {
					// console.log(e)
					this.keyword = e
					this.getSearchList()
					this.saveHistory()
				}, 1000)
			},
			async getSearchList() {
				// 当搜索框为空时，禁止发送请求
				if (this.keyword === '') {
					this.searchList = [];
					return
				}
				const {
					data: res
				} = await search(this.keyword);
				console.log(res)
				if (res.meta.status !== 200) return uni.$showMag()
				this.searchList = res.message
			},
			goGoodsList(goods_id) {
				// 点击列表，跳转到商品相应的商品详情页
				uni.navigateTo({
					url: '/pages-supper/goods_list/goods_list?goods_id=' + goods_id
				})
			},
			clear() {
				this.searchList = [];
			},
			
			saveHistory() {
				// 使用new Set 实现数组去重
				const set = new Set(this.history);
				set.delete(this.keyword);
				set.add(this.keyword);
				this.history = Array.from(set);
				uni.setStorageSync("keyword", JSON.stringify(this.history))
			},
			// 跳转到商品详细列表里
			goGoodsList(keyword) {
				uni.navigateTo({
					url: '/pages-supper/goods_list/goods_list?query=' + keyword
				})
			},
			
			clearHistory() {
				// 清空历史记录
				this.history = [];
				// 将本存储中的历史记录清空
				uni.setStorageSync('keyword', '[]');
			},
			
		}
	}
</script>

<style lang="scss">
	.search {
		background-color: #CB2D01;
		height: 120rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		.input {
			width: 800rpx;
		}

	}

	.search_list {

		.search_title {
			display: flex;
			justify-content: center;
			align-items: center;
			line-height: 80rpx;
			border-bottom: 1px solid #ccc;


			.search_goods_name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}

	.history {
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			line-height: 70rpx;
			border-bottom: 1px solid #ccc;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.history-name {
				margin: 10rpx 20rpx;
			}
		}
	}
</style>
